#!/usr/bin/env python
# encoding: utf-8
'''
@author: 王震
@license: 四川凡狼科技有限公司
@contact: 2668985108@qq.com
@file: interview.py
@time: 2022/1/13 9:16 下午
@desc:
'''

from app.msg.tips import *
from app.msg.resp import *
from main import db
from app.model.interview.interview import Interview, InterviewQuestions
from app.public.tool import create_key
from app.serialize.interview import InterviewObj, InterviewQuestionsObj


def add_interview(name) -> dict:
    """
    添加试题类型
    :param name: 试题名称
    :return: dict 
    """
    if not name:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    try:
        res = Interview.query.filter(Interview.name == name).count()
        if res:
            return response_json(code=tips['existence'], msg=tipsText['existence'])
        _interview = Interview()
        _interview.name = name
        _interview.id = create_key()
        db.session.add(_interview)
        db.session.commit()
        return response_json()
    except EOFError:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def get_system_interview(page_number, page_size) -> list:
    """
    管理系统获取面试题类型列表
    :param page_number: 当前页码
    :param page_size:  每一页显示的数据条数
    :return:
    """
    try:
        res = Interview.query.filter().paginate(int(page_number), int(page_size), False)
        item = res.items
        interview_obj = InterviewObj(many=True)
        data = interview_obj.dump(item)
        return response_list_json(data=data, page_number=res.page, page_size=res.pages)
    except EOFError:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def get_interview_list(page_number, page_size) -> list:
    """
      管理系统获取面试题类型列表
      :param page_number: 当前页码
      :param page_size:  每一页显示的数据条数
      :return:
      """
    try:
        res = Interview.query.filter(Interview.show == 1).paginate(int(page_number), int(page_size), False)
        item = res.items
        interview_obj = InterviewObj(many=True)
        data = interview_obj.dump(item)
        return response_list_json(data=data, page_number=res.page, page_size=res.pages)
    except EOFError:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def edit_interview(data):
    """
    编辑面试题类型
    :param data: 编辑对象
    :return:
    """
    if not data['id'] or not data['name'] or not len(data['id']) or not len(data['name']):
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    try:
        obj = {
            "name": data['name'],
            "id": data['id']
        }
        res = Interview.query.filter(Interview.id == data['id']).update(obj)
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
        return response_json()
    except EOFError:
        db.session.eoollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def del_interview(id):
    """
    删除面试题类型
    :param id: 类型id
    :return:
    """
    if not id or not len(id):
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    try:
        res = Interview.query.filter(Interview.id == id).delete()
        if res:
            return response_json()
        return response_json(code=tips['noResult'], msg=tipsText['noResult'])
    except EOFError:
        db.session.eoollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def add_interview_questions(data):
    """
    创建面试题
    :param data:
    :return:
    """
    arr = data.keys()
    if not ('details' in arr) or not len(data['details']) or not ('classificationId' in arr) or not len(
            data['classificationId']):
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    try:
        res = Interview.query.filter(Interview.id == data['classificationId']).count()
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
        interview_questions = InterviewQuestions()
        interview_questions.details = data
        interview_questions.id = create_key()
        db.session.add(interview_questions)
        db.session.commit()
        return response_json()
    except EOFError:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])


def get_interview_questions(page_number, page_size):
    """
    客户端获取面试题列表
    :param page_number: 页码
    :param page_size: 每一页显示的数据条数
    :return:
    """
    try:
        res = InterviewQuestions.query.filter().paginate(int(page_number), int(page_size), False)
        items = res.items
        interview_questions_obj = InterviewQuestionsObj()
        data = interview_questions_obj.dump(items)
        for item in data:
            _res = Interview.query.filter(Interview.id == item['classificationId']).first()
            interview_obj = InterviewObj()
            item['InterviewType'] = interview_obj.dump(_res)
        return response_list_json(data=data, page_number=items.page, page_size=items.pages)
    except EOFError:
        db.session.rollback()
        return response_json(code=tips['System_error'], msg=tipsText['System_error'])
